/*
**  File: AquaFuncts_diana.c
**
**  This file is part of the AQUA program suite, which is being
**  developed in the NMR Spectroscopy Department, Bijvoet Center for 
**  Biomolecular Research, Utrecht. 
**
**
**  AUTHOR(S) : Ton Rullmann
**  VERSION   : 1.1
**  DATE      : Feb 17, 1997
**
**  This file contains routines for handling Diana files.
**
**  Most header definitions are present in AquaFuncts_io.c
*/

/****************  function prototypes  ****************/

#include "AquaFuncts_diana.h"

/* BEGIN_EXTH */

/****************  WriteRestraints_diana  ****************/
/*
**  Write restraints in Diana restraint format. 
**  Diana format:
**  13 VAL
**          HN      13 VAL  HB      3.2   1.00E+00
**
**  13 VAL  HN      13 VAL  HB      3.2   1.00E+00
**
**  FORMAT(I3,1X,2A5,I4,1X,2A5,F7.2,2X,E8.2); the weight factor is optional.
*/

void WriteRestraints_diana( FILE *fp, DRestraint *DR_list, int DR_count, 
                            int skip_count, Card ident)
{
    char        *fct_name = "WriteRestraints_diana";
    int         count, phase = 0;
    float       what;
    DRestraint  *restrs;

    fprintf( fp, "#%s\n", ident );
    
    while ( ++phase <= 2 )
    {
	if ( phase == 1 )
	    fprintf( fp, "#UPPER BOUNDS\n" );
	else if ( phase == 2 )
	    fprintf( fp, "#LOWER BOUNDS\n" );
	count = DR_count;
	restrs = DR_list;
	while ( count-- )
	{
	    if ( !restrs->keep )
	    {
		restrs++;
		continue;
	    }
                        
	    switch( restrs->stat )
	    {
		case bnd_upper:
		    if ( phase == 2 ) {
			restrs++;
			continue;
		    }
		    what = restrs->up_bnd;
		    break;
		case bnd_lower:
		    if ( phase == 1 ) {
			restrs++;
			continue;
		    }
		    what = restrs->lo_bnd;
		    break;
		case bnd_uld: /* Why did we not program this option ??? */
		case bnd_uplo:
		    if ( phase == 1 ) {
			what = restrs->up_bnd;
                        break;
                    } 
                    if ( phase == 2 ) {
			what = restrs->lo_bnd;
                        break;
                    }
		    FATAL( "Code bug - in case structure" );
                    exit(99); /* Will never be executed */
		default:
		    FATAL( "wrong data type" );
                    exit(99); /* Will never be executed */
	    }

	    if ( LenChars( restrs->atom_A.chain, size_ID ) )
	    {
		printf( "* don't know how to handle chain id's" );
		return;
	    }
	    fprintf( fp, "%3i ", restrs->atom_A.res_num_orig );
	    if ( restrs->atom_A.res_insert != ' ' )
	    {
		printf( "* don't know how to handle residue insertion codes" );
		return;
	    }
	    FPrintChars( fp, restrs->atom_A.res_nam, 5 );
	    FPrintChars( fp, restrs->atom_A.atom_ext, 5 );


	    if ( LenChars( restrs->atom_B.chain, size_ID ) )
	    {
		printf( "* don't know how to handle chain id's" );
		return;
	    }
	    fprintf( fp, "%4i ", restrs->atom_B.res_num_orig );
	    if ( restrs->atom_B.res_insert != ' ' )
	    {
		printf( "* don't know how to handle residue insertion codes" );
		return;
	    }
	    FPrintChars( fp, restrs->atom_B.res_nam, 5 );
	    FPrintChars( fp, restrs->atom_B.atom_ext, 5 );

	    fprintf( fp, "%7.2f\n", what );
	    restrs++;
	}
    }
}
